package com.gmscosmo.janus.handler;

import com.alibaba.fastjson.JSONObject;
import com.gmscosmo.model.Node;
import com.gmscosmo.model.Producer;
import com.gmscosmo.model.RtpForwardData;

import java.util.Random;


/**
 *
 * ClassName: JanusVideoRoomHandler
 * Description: 
 * Date: 2021/10/12 11:07
 * @author stephen.qiu
 * @version 1.0
 *
 */
public abstract class JanusVideoRoomHandler extends JanusHandler {
    /**
     * 判断房间是否存在
     * @param roomExistData
     * @param nodeId
     * @return
     * @throws Exception
     */
    public abstract boolean roomExist(JSONObject roomExistData, int nodeId) throws Exception;

    /**
     * 创建房间
     * @param createRoomData
     * @param session_id
     * @param handle_id
     * @param nodeId
     * @return
     * @throws Exception
     */
    public abstract String createRoom(JSONObject createRoomData, long session_id, long handle_id, int nodeId) throws Exception;

    /**
     * 加入房间
     * @param joinData
     * @param type
     * @param nodeId
     * @return
     * @throws Exception
     */
    public abstract String join(JSONObject joinData, String type, int nodeId) throws Exception;

    /**
     * 发送offer
     * @param offerDate
     * @param nodeId
     * @return
     * @throws Exception
     */
    public abstract String sendOffer(JSONObject offerDate, int nodeId) throws Exception;

    /**
     * 发送候选者
     * @param candidateDate
     * @param nodeId
     * @return
     * @throws Exception
     */
    public abstract String sendCandidate(JSONObject candidateDate, int nodeId) throws Exception;

    /**
     * 保持活跃
     * @param keepaliveData
     * @param nodeId
     * @return
     * @throws Exception
     */
    public abstract String keepalive(JSONObject keepaliveData, int nodeId) throws Exception;

    /**
     * 离开房间
     * @param leaveData
     * @param nodeId
     * @return
     * @throws Exception
     */
    public abstract String leave(JSONObject leaveData, int nodeId) throws Exception;

    /**
     * 销毁房间
     * @param destroyData
     * @param nodeId
     * @return
     * @throws Exception
     */
    public abstract String destroy(JSONObject destroyData, int nodeId) throws Exception;

    /**
     * 媒体协商
     * @param configureData
     * @param nodeId
     * @return
     * @throws Exception
     */
    public abstract String configure(JSONObject configureData, int nodeId) throws Exception;

    /**
     * 开始rtp转发
     * @param rtpForwardData
     * @param userId
     * @param nodeId
     * @return
     * @throws Exception
     */
    public abstract String rtpForward(JSONObject rtpForwardData, String userId, int nodeId) throws Exception;

    /**
     * 停止我对他人rtp转发器
     * @param userId
     * @throws Exception
     */
    public abstract void stopMyRtpForward(String userId) throws Exception;

    /**
     * 停止他人对我rtp转发
     * @param rtpForwardFrom
     * @param rtpForwardTo
     * @return
     * @throws Exception
     */
    public abstract void stopOtherRtpForward(String rtpForwardFrom, String rtpForwardTo) throws Exception;

    /**
     * 重新协商ice
     * @param restartICEData
     * @param nodeId
     * @return
     * @throws Exception
     */
    public abstract String sendRestartICE(JSONObject restartICEData, int nodeId) throws Exception;

    /**
     * 添加生产者
     * @param session_id
     * @param handle_id
     * @param host
     * @param streamId
     * @param room
     * @param publisherId
     * @param audio_port
     * @param video_port
     * @param forwardTo
     * @param audioStreamId
     * @param videoStreamId
     */
    protected void addProducer(long session_id,long handle_id,String originHost,String host,int streamId,int room, long publisherId, Integer audio_port, Integer video_port,Integer video_rtcp_port,String forwardTo,String rtpForwardFrom, long audioStreamId, long videoStreamId) {
        Producer producer = new Producer();
        producer.setSession_id(session_id);
        producer.setHandle_id(handle_id);
        producer.setOriginHost(originHost);
        producer.setHost(host);
        producer.setStreamId(streamId);
        producer.setRoom(room);
        producer.setRtpForwardTo(forwardTo);
        producer.setAudio_port(audio_port);
        producer.setVideo_port(video_port);
        producer.setId(publisherId);
        producer.setAudioStreamId(audioStreamId);
        producer.setVideoStreamId(videoStreamId);
        producer.setVideo_rtcp_port(video_rtcp_port);
        janusUtil.putProducers(rtpForwardFrom, producer);
    }

    /**
     * 创建rtp转发请求
     * @param userId
     * @return
     */
    public RtpForwardData createRtpForwardRequest(String userId) {
        RtpForwardData rtpForwardData = new RtpForwardData();
        rtpForwardData.setForwardFrom(userId);
        Random random = new Random();
        int id = random.nextInt(9999999) + 1000000;
        rtpForwardData.setId(id);
        Node node = janusUtil.getNodeInfo(janusUtil.getUserNode(userId));
        rtpForwardData.setClusterId(node.getClusterId());
        rtpForwardData.setHost(node.getHost());
        rtpForwardData.setInterHost(node.getInterHost());
        rtpForwardData.setAudio_port(janusUtil.getPort());
        rtpForwardData.setVideo_port(janusUtil.getPort());
        rtpForwardData.setVideo_port_2(janusUtil.getPort());
        rtpForwardData.setVideo_port_3(janusUtil.getPort());
        rtpForwardData.setVideo_rtcp_port(janusUtil.getPort());
        return rtpForwardData;
    }
}
